(function ($) {

    var methods = {

        init: function () {
            var t = $(this), args = arguments;
            // Check whether we have already created a buttonset for this element
            var buttonset = this.data('button');
            if (buttonset && buttonset.length !== 0)
                return this;

            var buttonset = $('<div class="ui-element ui-radio-buttonset "></div>');
            buttonset.delegate('div.ui-radio-button', 'click',
				(function (buttonset, callback) {
				    return function (e) { switchActive(e, buttonset, callback); }
				})(buttonset, args[0]));

            var inputs = this.find('input')
            inputs.each(function (i, input) {
                input = $(input);
                // If we use iframes, we need to sure
                // that we are looking in the right body
                var label = input.parent().find('label[for="' +
					input.attr('id') + '"]');

                var button = $('<div _ui_value="' + input.attr('value') +
					'" class="ui-button ui-radio-button ui-radio-enabled">' +
					label.text() + '</div>');

                // Check the default value
                if (input.attr('checked'))
                    button.addClass('ui-radio-checked');

                // Remove the right border for visual appearance
                if (i < inputs.length - 1)
                    button.addClass('ui-radio-removeborder');

                buttonset.append(button);
            });

            // Hide the original, save and memorise the fake
            t.hide();
            t.last().after(buttonset);
            t.data('buttonset', buttonset);

            function switchActive(e, buttonset, callback) {
                var t = $(e.target), checkClass = 'ui-radio-checked';
                // Check whether the target element is already active or disabled
                if (t.hasClass(checkClass) ||
						t.hasClass('ui-radio-disabled'))
                    return;

                // Uncheck the currently active element
                buttonset.find('div.' + checkClass).removeClass(checkClass);
                t.addClass(checkClass);

                if (callback)
                    callback(t.attr('_ui_value'));
            }
        },

        enable: function () {
            var t = $(this), buttonset = t.data('buttonset');
            buttonset.find('div').addClass('ui-radio-enabled');
            buttonset.find('div').removeClass('ui-radio-disabled');
            t.find('input').removeAttr('disabled');
            return this;
        },

        disable: function () {
            var t = $(this), buttonset = t.data('buttonset');
            buttonset.find('div').addClass('ui-radio-disabled');
            buttonset.find('div').removeClass('ui-radio-enabled');
            this.find('input').attr('disabled', 'disabled');
            return this;
        },

        destroy: function () {
            var t = $(this), buttonset = t.data('buttonset');
            buttonset.remove();
            this.data('buttonset', '');
            this.show();
            return this;
        }
    };

    $.fn.radio = function (method) {
        // Choose the correct action
        if (methods[method]) {
            return methods[method].apply(this, arguments);
        } else if (typeof method === 'object' || typeof method === 'function'
                || !method) {
            return methods.init.apply(this, arguments);
        }
    };

})(jQuery);
